package jee.boot.entity.auth;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jee.boot.common.annotation.Column;
import jee.boot.common.annotation.Table;
import jee.boot.common.annotation.Transient;
import jee.boot.common.basic.interceptor.impl.DefaultPreInsertInterceptor;
import jee.boot.common.basic.interceptor.impl.DefaultPreQueryInterceptor;
import jee.boot.common.basic.interceptor.impl.DefaultPreUpdateInterceptor;
import jee.boot.entity.system.BasicObj;
import jee.boot.entity.system.GeneralDataEntity;

/**
 * 原子权限与实体关系
 */
@Table(name = "at_r_auth_entity")
public class AuthEntity extends GeneralDataEntity<AuthEntity> {

    private static final long serialVersionUID = 1L;

    /**
     * 原子权限ID
     */
    @Column(name = "st_authority_id", data = {"authority.id", "authority.no", "authority.code", "authority.name", "authority.desc", "authority.type", "authority.authorityStatus"})
    private Authority authority = new Authority();//引用：AT_AUTHORITY
    /**
     * 受控实体类型
     */
    @Column(name = "st_entity_type")
    private String entityType;//字典：DIC_AUTH_CtrledEntityType
    /**
     * 受控实体ID
     */
    @Column(name = "st_entity_id")
    private String entityId;//引用：ST_RESOURCE、ST_REQUEST、ST_TREE_NODE、ST_DATASET,具体引用哪个表取决于ST_ENTITY_TYPE值
    /**
     * 操作等级ID,默认可见权限
     */
    @Column(name = "st_operation_level_id", data = {"operationLevel.id", "operationLevel.priority", "operationLevel.ruleId", "operationLevel.ruleKeyword", "operationLevel.noPermDefaultFlag", "operationLevel.noSpecDefaultFlag", "operationLevel.systemUsedFlag"})
    private OperationLevel operationLevel = new OperationLevel("20");//引用：AT_OPERATION_LEVEL
    /**
     * 字典：DIC_AUTH_ControlDomainType，当前关系的作用范围类型，默认为DIC_AUTH_ControlDomainType_CDT01无控制(全局有效)。但是在设定模型权限时通常采用DIC_AUTH_ControlDomainType_CDT03请求控制。
     */
    @Column(name = "st_control_domain_type")
    private String controlDomainType;
    /**
     * 动态引用，当ST_CONTROL_DOMAIN_TYPE='DIC_AUTH_ControlDomainType_CDT01'时此字段为空；当ST_CONTROL_DOMAIN_TYPE='DIC_AUTH_ControlDomainType_CDT03'时此字段为请求ID。
     */
    @Column(name = "st_control_domain_id", data = {"controlDomain.id", "controlDomain.title", "controlDomain.name", "controlDomain.sort"})
    private Resource controlDomain = new Resource("global");

    /**
     * 实体数据
     */
    @Transient
    private Object entity;
    @Transient
    private Integer defSort = 0;

    /**
     * 冗余查询使用的数据过滤字段
     */
    @Column(name = "st_model_id", data = {"modelData.id", "modelData.no", "modelData.code", "modelData.name", "modelData.desc", "modelData.type", "modelData.modelTableName"})
    private ModelData modelData = new ModelData();
    @Column(name = "st_rowset_id")
    private RowSet rowSet = new RowSet();
    @Column(name = "st_column_id")
    private jee.boot.entity.auth.Column columnData = new jee.boot.entity.auth.Column();


    public AuthEntity() {
    }

    public AuthEntity(String id) {
        super(id);
    }

    public AuthEntity(Authority authority) {
        this.authority = authority;
    }

    public AuthEntity(OperationLevel operationLevel) {
        this.operationLevel = operationLevel;
    }

    public AuthEntity(String entityType, BasicObj entity) {
        this.entityType = entityType;
        this.entity = entity;
    }

    public Authority getAuthority() {
        return authority;
    }

    public void setAuthority(Authority authority) {
        this.authority = authority;
    }

    public String getEntityType() {
        return entityType;
    }

    public void setEntityType(String entityType) {
        this.entityType = entityType;
    }

    public String getEntityId() {
        return entityId;
    }

    public void setEntityId(String entityId) {
        this.entityId = entityId;
    }

    public OperationLevel getOperationLevel() {
        return operationLevel;
    }

    public void setOperationLevel(OperationLevel operationLevel) {
        this.operationLevel = operationLevel;
    }

    public String getControlDomainType() {
        return controlDomainType;
    }

    public void setControlDomainType(String controlDomainType) {
        this.controlDomainType = controlDomainType;
    }

    public Resource getControlDomain() {
        return controlDomain;
    }

    public void setControlDomain(Resource controlDomain) {
        this.controlDomain = controlDomain;
    }

    public Object getEntity() {
        return entity;
    }

    public void setEntity(Object entity) {
        this.entity = entity;
    }

    public ModelData getModelData() {
        return modelData;
    }

    public void setModelData(ModelData modelData) {
        this.modelData = modelData;
    }

    public RowSet getRowSet() {
        return rowSet;
    }

    public void setRowSet(RowSet rowSet) {
        this.rowSet = rowSet;
    }

    public jee.boot.entity.auth.Column getColumnData() {
        return columnData;
    }

    public void setColumnData(jee.boot.entity.auth.Column columnData) {
        this.columnData = columnData;
    }

    @JsonIgnore
    public Integer getDefSort() {
        return defSort;
    }

    public void setDefSort(Integer defSort) {
        this.defSort = defSort;
    }

    @Override
    public void preInsert() {
        new DefaultPreInsertInterceptor().execute(this);
    }
    @Override
    public void preUpdate() {
        new DefaultPreUpdateInterceptor().execute(this);
    }
    @Override
    public void preQuery() {
        new DefaultPreQueryInterceptor().execute(this);
    }
}
